/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.news.presenter;

import com.yiqi.liebang.entity.bo.IssuesBo;
import com.yiqi.liebang.feature.news.contract.IssuesContract;
import com.yiqi.liebang.feature.news.dependencies.issues.DaggerIssuesPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class IssuesPresenter implements IssuesContract.Presenter {

    private IssuesContract.View mView;

    @Inject

    IssuesContract.Model mModel;


    public IssuesPresenter(IssuesContract.View mView) {
        this.mView = mView;
        DaggerIssuesPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getIssuesBo() {
        mModel.getIssuesBo().compose(mView.bindToLife()).subscribe(new Observer<List<IssuesBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<IssuesBo> issuesBos) {
                if (issuesBos != null && issuesBos.size() > 0) {
                    mView.showIssuesSuccess(issuesBos);
                } else {
                    mView.showEmpty();
                }
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showIssuesError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getRefuseFriend(String id) {
        mModel.getRefuseFriend(new IssuesBo(id)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String issuesBos) {
                mView.showRefuseFriendSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPassFriend(String id) {
        mModel.getPassFriend(new IssuesBo(id)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String issuesBos) {
                mView.showPassFriendSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
